import { DocxGenerator } from './index';
import { ProcessBar, Image, Link } from './components';
import { Echart } from './components';
import * as fs from 'fs';
// import * as http from 'http';

const main = () => {
    const docxGenerator = new DocxGenerator();
    // 模版
    const buffer = fs.readFileSync('./template/simple.docx');

    // 数据
    const img = new Image(200, 200, 'Kung Fu Hero');
    img.setPath('./template/image/kongfu.png');

    // const img2 = new Image(100, 100, 'test');
    // img2.setPath('http://wiki.xiaofangyi.com:8090/images/icons/profilepics/Avatar-4.png');

    const chart = new Echart(500, 400);
    chart.setOptions({
        xAxis: {
            type: 'category',
            data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
        },
        yAxis: {
            type: 'value'
        },
        series: [
            {
                data: [150, 230, 224, 218, 135, 147, 260],
                label: {show: true},
                type: 'line'
            }
        ]
    });

    const pieChart = new Echart(500, 400);
    pieChart.setOptions({
        tooltip: {
            trigger: 'item',
        },
        legend: {
            orient: 'vertical',
            bottom: 'bottom',
        },
        series: [
            {
                type: 'pie',
                radius: '50%',
                label: {
                    show: true,
                    color: '#000000',
                    position: 'outside',
                },
                data: [
                    {
                        "name": "消防系统故障1",
                        "value": 2
                    },
                    {
                        "name": "遮挡类21",
                        "value": 2
                    },
                    {
                        "name": "可燃物2",
                        "value": 2
                    },
                    {
                        "name": "消火栓3",
                        "value": 2
                    },
                    {
                        "name": "防火门2",
                        "value": 2
                    }
                ]
            },
        ],
    })
    

    // 生成文档 方式一
    docxGenerator.render(buffer, {
        lineChart: chart,
        pieChart: pieChart,
        first_name: 'First Name_-',
        last_name: 'Last Name',
        description: '描述',
        phone: 'okok',
        "Kung Fu Hero": img,
        // "test": img2,
        "Beers": [
            { "Brand": 'Carlsberg', "Price": 1, bars: [
                {
                    bar: new ProcessBar(0.3)
                },
                {
                    bar: new ProcessBar(0.3)
                },
                {
                    bar: new ProcessBar(0.3)
                }
            ] }
        ],
        "easy": new Link('https://github.com/alonrbar/easy-template-x', 'super easy')
    }, {
        output: './docx',
        fileName: 'test',
    });


     // 生成文档 方式二
    //  http.createServer((req, res) => {
        
    //     const fileName = '测试文档';
    //     res.setHeader('Content-Type', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document');
    //     res.setHeader('Content-Disposition', "attachment; filename=\"".concat(encodeURIComponent(fileName), "\"; filename*=utf-8''").concat(encodeURIComponent(fileName), ";"))
    //     docxGenerator.render(buffer, {
    //         lineChart: chart,
    //         first_name: 'First Name_-',
    //         last_name: 'Last Name',
    //         description: '描述',
    //         phone: 'okok',
    //         "Kung Fu Hero": img,
    //         "test": img2,
    //         "Beers": [
    //             { "Brand": 'Carlsberg', "Price": 1, bar: new ProcessBar(0.3) },
    //             { "Brand": "Leaf Blonde", "Price": 2, bar: new ProcessBar(0.4) },
    //             { "Brand": "Weihenstephan", "Price": 1.5, bar: new ProcessBar(0.9) }
    //         ],
    //         "easy": new Link('https://github.com/alonrbar/easy-template-x', 'super easy')
    //     }, {
    //         renderer: 'stream',
    //         stream: res,
    //         output: './docx',
    //         fileName: 'aa',
    //     });
    //  }).listen(3100);

}

main();